package com.xiyu.service.repository.system.user;

import com.xiyu.service.vo.system.user.user.*;
import com.xiyu.service.model.system.dept.SystemDeptFetcher;
import com.xiyu.service.model.system.dept.SystemUserPostTable;
import com.xiyu.service.model.system.user.SystemUser;
import com.xiyu.service.model.system.user.SystemUserFetcher;
import com.xiyu.service.model.system.user.SystemUserTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

public interface SystemUserRepository extends JRepository<SystemUser, Long>{

    SystemUserTable systemUserTable = SystemUserTable.$;

    default Page<SystemUser> getUserPage(UserPageInput reqVO){
        return sql().createQuery(systemUserTable)
                .where(systemUserTable.username().likeIf(reqVO.getUsername()))
                .where(systemUserTable.mobile().likeIf(reqVO.getMobile()))
                .where( systemUserTable.status().eqIf(reqVO.getStatus()))
                .whereIf(reqVO.getCreateTime() != null,  () -> systemUserTable.createTime().between(reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]))
                .where(systemUserTable.id().ge(0L))
                .select(systemUserTable.fetch(SystemUserFetcher.$.allScalarFields().dept(SystemDeptFetcher.$.allScalarFields())))
                .fetchPage(reqVO.getPageNo() - 1, reqVO.getPageSize());
    }

    default List<SystemUser> getExportUserList(UserExportedInput reqVO){
        return sql()
                .createQuery(systemUserTable)
                .where(systemUserTable.username().likeIf(reqVO.getUsername()))
                .where(systemUserTable.mobile().likeIf(reqVO.getMobile()))
                .where( systemUserTable.status().eqIf(reqVO.getStatus()))
                .whereIf(reqVO.getCreateTime() != null,  () -> systemUserTable.createTime().between(reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]))
                .select(systemUserTable.fetch(SystemUserFetcher.$.allScalarFields().dept(SystemDeptFetcher.$.allScalarFields()))).execute();
    }

    default Optional<SystemUser> GetUser(long id){
        return sql()
                .createQuery(systemUserTable)
                .where(systemUserTable.id().eq(id))
                .select(systemUserTable.fetch(SystemUserFetcher.$.allScalarFields().dept(SystemDeptFetcher.$.allScalarFields())))
                .fetchOptional();
    }

    default List<SystemUser> GetUserListByStatus(Integer status){
        return sql().createQuery(systemUserTable).where(systemUserTable.status().eq(status)).select(systemUserTable).execute();
    }

    default void UpdateUserPassword(long id, String password){
        sql().createUpdate(systemUserTable).set(systemUserTable.password(), password).where(systemUserTable.id().eq(id)).execute();
    }

    default void UpdateUserStatus(long id,int status){
        sql().createUpdate(systemUserTable).set(systemUserTable.status(), status).where(systemUserTable.id().eq(id)).execute();
    }

    default void UpdateUserLogin(long id, String loginIp){
        sql().createUpdate(systemUserTable).set(systemUserTable.loginIp(), loginIp).set(systemUserTable.loginDate(), LocalDateTime.now()).where(systemUserTable.id().eq(id)).execute();
    }

    default void UpdateUserAvatar(long id, String avatar){
        sql().createUpdate(systemUserTable).set(systemUserTable.avatar(), avatar).set(systemUserTable.loginDate(), LocalDateTime.now()).where(systemUserTable.id().eq(id)).execute();
    }

    default List<SystemUser> getUserListByPostIds(Collection<Long> postIds){
        return sql().createQuery(SystemUserPostTable.$).where(SystemUserPostTable.$.id().in(postIds)).select(SystemUserPostTable.$.user()).execute();
    }

    default List<Long> findIdByDeptId(Long deptId){
        return sql().createQuery(systemUserTable)
                .where(systemUserTable.deptId().eq(deptId))
                .select(systemUserTable.id())
                .execute();
    }

    default List<Long> findIdByDeptIds(List<Long> deptIds){
        return sql().createQuery(systemUserTable)
                .where(systemUserTable.deptId().in(deptIds))
                .select(systemUserTable.id())
                .execute();
    }

    Optional<SystemUser> findByUsername(String username);

    Optional<SystemUser> findByMobile(String mobile);

    Optional<SystemUser> findByEmail(String email);

    List<SystemUser> findByNickname(String nickname);

    List<SystemUser> findByDeptIdIn(Collection<Long> deptIds);

    List<SystemUser> findByDeptId(Long deptId);


}
